package com.householditemtwo.admin.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.householditemtwo.common.entity.Ly;
import com.householditemtwo.common.entity.Product;
import com.householditemtwo.common.utils.DBManager;
import com.householditemtwo.common.utils.QueryDataResult;

/**
 * 用户DAO
 * @author admin
 *
 */
	public class LyDao {
			
		/**
		 * 查询所有产品的总条数(分页)
		 * @return
		 */
			public int count(String lyname) {
				DBManager db=new DBManager();
				String sql="select count(*) from ly ";
				if (lyname !=null && lyname !="" ) {
					sql+="where lyname like '%"+lyname+"%'";
				}
				QueryDataResult qdr=db.executeQuery(sql);
			int total=0;
				try {
					while(qdr.rs.next()) {
					total=qdr.rs.getInt(1);
					}
				}catch(SQLException e) {
					e.printStackTrace();
				}
				//返回查询的结果
				return total;
			}
			
		/**
		 * 查询所有产品
		 * @return
		 */
			public List<Ly> selectAllProduct( int page, int pageSize,String lyname) {
				DBManager db=new DBManager();
				String sql="select * from ly ";
						
					if (lyname !=null && lyname !="" ) {
							sql  +=" where lyname like '%"+lyname+"%'";
					}
					sql+= " order by lyid limit "+pageSize*(page-1)+","+pageSize;
				QueryDataResult qdr=db.executeQuery(sql);
				Product emp=new Product();
				ArrayList<Ly> list=new ArrayList<Ly>();
				try {
					while(qdr.rs.next()) {
						Ly c=new Ly();
						c.setLyid(qdr.rs.getInt(1));
						c.setLycontent(qdr.rs.getString(2));
						c.setLytime(qdr.rs.getString(3));
						c.setLyname(qdr.rs.getString(4));
						c.setLyphone(qdr.rs.getString(5));
						c.setLyemail(qdr.rs.getString(6));
						c.setLystate(qdr.rs.getString(7));
						list.add(c);
					}
				}catch(SQLException e) {
					e.printStackTrace();
				}
				//返回查询的结果
				return list;
			}
			
			
			
		/**
		 * 删除产品
		 * @return
		 */
			public int delete(int id) {
				//拼接sql
				String sql= "update ly set lystate='已读'   where lyid="+id;
						
				//执行sql
			return	DBManager.executeUpdate(sql);
			}
	
}
